﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace PuzzleDulex.bamboo
{
    public class Square
    {
        public const int squareLen = 155;
        public const int squareSource = 200;
        public const int PADDING = 5;

        public TILE tile;

        public int X;
        public int Y;

        public int Xpos;
        public int Ypos;

        public int index;

        public Rectangle rectToDraw;
        public Rectangle rectSourceToDraw;

        public Square(TILE tile, int index)
        {
            this.tile = tile;
            this.index = index;

            this.X = index % PuzzleState.BOARD_WIDTH;
            this.Y = index / PuzzleState.BOARD_WIDTH;

            this.Xpos = X * squareLen;
            this.Ypos = Y * squareLen;

            getRectangleDraw();
            getSourceRectangleDraw();
        }

        public void resetAttributes()
        {

            this.X = index % PuzzleState.BOARD_WIDTH;
            this.Y = index / PuzzleState.BOARD_WIDTH;
            this.Xpos = X * squareLen;
            this.Ypos = Y * squareLen;
            this.getRectangleDraw();
            this.getSourceRectangleDraw();
        }

        public bool isTouchInside(int x, int y)
        {
            bool touched = false;
            Rect squareRect = new Rect(Xpos + PADDING + Board.BOARD_MARGIN_LEFT, Ypos + PADDING + Board.BOARD_MARGIN_TOP, squareLen - PADDING, squareLen - PADDING);
            touched = squareRect.isPointInRect(x, y);

            return touched;
        }


        /// <summary>
        /// Use to get Rectangle Source to draw
        /// </summary>
        /// <param name="tile"></param>
        /// <returns></returns>
        int getIndexFromTile(TILE tile)
        {
            switch (tile)
            {
                case TILE.TL_1:
                    return 0;
                case TILE.TL_2:
                    return 1;
                case TILE.TL_3:
                    return 2;
                case TILE.TL_4:
                    return 5;
                case TILE.TL_5:
                    return 8;
                case TILE.TL_6:
                    return 7;
                case TILE.TL_7:
                    return 6;
                case TILE.TL_8:
                    return 3;
                case TILE.TL_SPACE:
                    return 4;

            }

            return (-1);
        }


        public bool isNearSquare(Square checkSquare)
        {
            int nearX = Math.Abs(this.X - checkSquare.X);
            int nearY = Math.Abs(this.Y - checkSquare.Y);

            return ((nearX + nearY) == 1);
        }

        public void getRectangleDraw()
        {
            rectToDraw = new Rectangle(Xpos + PADDING + Board.BOARD_MARGIN_LEFT, Ypos + PADDING + Board.BOARD_MARGIN_TOP, squareLen - PADDING, squareLen - PADDING);

        }

        public void getSourceRectangleDraw()
        {
            int indexOfTile = this.getIndexFromTile(this.tile);
            if (indexOfTile > -1)
            {
                int xPosSource = indexOfTile % PuzzleState.BOARD_WIDTH;
                int yPosSource = indexOfTile / PuzzleState.BOARD_WIDTH;
                rectSourceToDraw = new Rectangle(xPosSource * squareSource, yPosSource * squareSource, squareSource, squareSource);
            }
        }

    }
}
